﻿using System.Data.SqlClient;
using Dapper;

namespace testWX.Data
{
    /// <summary>
    /// 
    /// </summary>
    public class EmployeeProcessor : IEmployeeProcessor
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly string _connectionString;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString"></param>
        public EmployeeProcessor(string connectionString)
        {
            _connectionString = connectionString;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employee"></param>
        public void Create(Employee employee)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Execute(
                    "insert into employee (first_name, last_name, address, home_phone, cell_phone) values (@FirstName, @LastName, @Address, @HomePhone, @CellPhone)",
                    new
                    {
                        employee.FirstName,
                        employee.LastName,
                        employee.Address,
                        employee.HomePhone,
                        employee.CellPhone
                    });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employeeId">Employee类对象的的id</param>
        public void Delete(int employeeId)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Execute("DELETE FROM Employee WHERE id=@Id",
                    new { Id = employeeId });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employee">Employee类对象</param>
        public void Update(Employee employee)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Execute("UPDATE Employee SET first_name =@FirstName, last_name =@LastName, address=@Address, home_phone=@HomePhone, cell_phone=@CellPhone WHERE id=@Id",
                    new { employee.FirstName, employee.LastName, employee.Address, employee.HomePhone, employee.CellPhone, employee.Id });
            }
        }
    }
}
